package br.com.yahoo.mau_mss.tutorjavafxgolfx.controller;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.Reflection;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
 * LoginApp
 * 12:58:00
 */
public class LoginApp extends Application {
  private static final String TITLE_LOGIN = "Login - GolFX";
  private BorderPane borderPane;
  private GridPane gridPane;
  private Label lbLogin;
  private TextField txLogin;
  private Label lbSenha;
  private PasswordField txSenha;
  private Label lbMensagem;
  private Button btEntrar;
  private Button btSair;
  private HBox hBox;
  private Reflection reflection;
  private DropShadow dropShadow;
  private Text text;
  private static Stage stage;
  private static final String PASSWORD = "admin";
  private static final String USERNAME = "admin";
  
  @Override
  public void start(Stage stage) throws Exception {
    initComponents();
    initListeners();
    Scene scene = new Scene(this.borderPane);
    scene.getStylesheets().add(getClass().getClassLoader().getResource("styles/login.css").toExternalForm());
    stage.setScene(scene);
    stage.setResizable(false);
    stage.setTitle(LoginApp.TITLE_LOGIN);
    stage.show();
    LoginApp.stage = stage;
  }
  
  private void initComponents() {
    this.borderPane = new BorderPane();
    this.borderPane.setPadding(new Insets(10,50,50,50));
    // Adding HBox
    this.hBox = new HBox();
    this.hBox.setPadding(new Insets(20,20,20,30));
    //Adding GridPane
    this.gridPane = new GridPane();
    this.gridPane.setPadding(new Insets(20,20,20,20));
    this.gridPane.setHgap(5);
    this.gridPane.setVgap(5);
    //Implementing Nodes for GridPane
    this.lbLogin = new Label("Login");
    this.txLogin = new TextField();
    this.txLogin.setPromptText("Digite aqui seu login");
    this.lbSenha = new Label("Senha");
    this.txSenha = new PasswordField();
    this.txSenha.setPromptText("Digite aqui sua senha");
    this.btEntrar = new Button("Entrar");
    this.btEntrar.getStyleClass().add("btEntrar");
    this.btSair = new Button("Sair");
    this.btSair.getStyleClass().add("btSair");
    this.lbMensagem = new Label();
    //Adding Nodes to GridPane layout
    this.gridPane.add(this.lbLogin, 0, 0); // linha 0
    this.gridPane.add(this.txLogin, 1, 0);
    this.gridPane.add(this.lbSenha, 0, 1); // linha 1
    this.gridPane.add(this.txSenha, 1, 1);
    this.gridPane.add(this.btEntrar, 2, 1);
    this.gridPane.add(this.btSair, 2, 2); // linha 2
    this.gridPane.add(this.lbMensagem, 1, 2);
    //Reflection for gridPane
    this.reflection = new Reflection();
    this.reflection.setFraction(0.7f);
    this.gridPane.setEffect(this.reflection);
    //DropShadow effect
    this.dropShadow = new DropShadow();
    this.dropShadow.setOffsetX(5);
    this.dropShadow.setOffsetY(5);
    //Adding text and DropShadow effect to it
    this.text = new Text(LoginApp.TITLE_LOGIN);
    this.text.setFont(Font.font("Courier New", FontWeight.BOLD, 28));
    this.text.setEffect(this.dropShadow);
    
    //Adding text to HBox
    this.hBox.getChildren().add(text);    
    addIdsToNodes();
    //Add HBox and GridPane layout to BorderPane Layout
    this.borderPane.setTop(this.hBox);
    this.borderPane.setCenter(this.gridPane);
  }
  
  private void initListeners() {
    this.btSair.setOnAction(new EventHandler<ActionEvent> () {
      @Override
      public void handle(ActionEvent t) {
        fecharAplicacao();
      }
    });
    this.btEntrar.setOnAction(new EventHandler<ActionEvent> () {
      @Override
      public void handle(ActionEvent t) {
        logar();
      }
    });
  }
  
  private void fecharAplicacao() {
    //System.exit(0);
    Platform.exit();
  }
  
  private void logar() {
    if (this.txLogin.getText().equals(LoginApp.USERNAME) && this.txSenha.getText().equals(LoginApp.PASSWORD)) {
      try {
        this.lbMensagem.setText("Correto!");
        this.lbMensagem.setTextFill(Color.GREEN);
        new VitrineApp().start(new Stage());
        LoginApp.stage.close();
      } catch (Exception ex) {
        Logger.getLogger(LoginApp.class.getName()).log(Level.SEVERE, null, ex);
      }
    } else {
      this.lbMensagem.setText("Incorreto");
      this.lbMensagem.setTextFill(Color.RED);
      this.txLogin.setText("");
      this.txSenha.setText("");
      JOptionPane.showMessageDialog(null, "Login e/ou senha inválidos", "Erro", JOptionPane.ERROR_MESSAGE);
    }
  }
  
  private void addIdsToNodes() {
    this.borderPane.setId("bp");
    this.gridPane.setId("root");
    this.btEntrar.setId("btnLogin");
    this.btSair.setId("btnExit");
    this.text.setId("text");
  }
  
}
